package com.caishi.lkx.exam.service.impl;

import com.caishi.lkx.course.model.VideoChapterModel;
import com.caishi.lkx.course.service.IVideoChapterService;
import com.caishi.lkx.exam.mapper.ExColumnConfigReMapper;
import com.caishi.lkx.exam.model.ChapterModel;
import com.caishi.lkx.exam.model.FreeModel;
import com.caishi.lkx.exam.service.IChapterService;
import com.caishi.lkx.exam.service.IColumnService;
import com.caishi.lkx.exam.service.IExColumnConfigReService;
import com.zzw.common.entity.IBSMapper;
import com.caishi.lkx.exam.ienum.type.ColumnType;

import com.caishi.lkx.exam.model.re.ExColumnConfigReModel;

import com.zzw.common.entity.IBaseEntity;
import com.zzw.common.service.model.base.SortModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author: Janisa
 * @date: 2022/3/17 17:53
 * @description:
 */
@Service
public class ExColumnConfigReServiceImpl implements IExColumnConfigReService {
    @Resource
    private ExColumnConfigReMapper mapper;

    @Resource
    private IChapterService chapterService;

    @Resource
    private IVideoChapterService videoChapterService;

    @Resource
    private IColumnService columnService;

    @Override
    public IBSMapper<ExColumnConfigReModel, Long> getMapper() {
        return mapper;
    }

    @Override
    public boolean configIsFree(String columnId, String configId) {
        var re = configData(columnId, configId);
        return re != null && re.getIsFree();
    }

    @Override
    public ExColumnConfigReModel configData(String columnId, String configId) {
        return selectFirst(wrappers().eq(ExColumnConfigReModel::getColumnId, columnId).eq(ExColumnConfigReModel::getConfigId, configId));
    }

    @Override
    public List<ChapterModel> getChaptersByColumn(String columnId) {
        return getItemsByColumn(columnId, chapterService::selectBatchIds);
    }

    @Override
    public List<VideoChapterModel> getVideoChaptersByColumn(String columnId) {
       return getItemsByColumn(columnId, videoChapterService::selectBatchIds);
    }

    private <T extends IBaseEntity & FreeModel & SortModel> List<T> getItemsByColumn(String columnId, Function<List<String>, List<T>> selectBatchIds) {
        List<ExColumnConfigReModel> re = mapper.selectList(wrappers().eq(ExColumnConfigReModel::getColumnId, columnId));
        if (re.isEmpty()) return Collections.emptyList();

        Map<String, ExColumnConfigReModel> reMap = re.stream()
                .collect(Collectors.toMap(ExColumnConfigReModel::getConfigId, v -> v));

        List<T> items = selectBatchIds.apply(re.stream().map(ExColumnConfigReModel::getConfigId).toList());

        return items.stream()
                .peek(e -> e.setFree(reMap.get(e.getId()).getIsFree()))
                .sorted(ColumnServiceImpl.reComparator(reMap))
                .toList();
    }

    @Override
    public List<String> columnTypeConfigs(Long categoryId, ColumnType columnType) {
        var columnIds = columnService.categoryColumn(categoryId, columnType);
        if (columnIds.isEmpty()) return columnIds;
        return selectList(wrappers()
                .select(ExColumnConfigReModel::getConfigId)
                .in(ExColumnConfigReModel::getColumnId, columnIds)
        ).stream().map(ExColumnConfigReModel::getConfigId).toList();
    }

}
